﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using VideoMonitorSystem.Model;
using System.Data.SqlClient;

namespace VideoMonitorSystem.DAL
{
     /// <summary>
     /// 展示计划数据层处理类
     /// </summary>
    public class ShowPlanService
    {
        private static ShowPlan GetShowPlanByDataRow(DataRow row)
        {
            ShowPlan showPlan = new ShowPlan();
            showPlan.PID = Convert.ToInt32(row["pID"].ToString());
            showPlan.PName = (string)row["pName"].ToString();
            showPlan.PDesc = (string)row["pDesc"].ToString();
            showPlan.PBegin = Convert.ToDateTime(row["pBegin"].ToString());
            showPlan.PEnd = Convert.ToDateTime(row["pEnd"].ToString());
            showPlan.PInTime = Convert.ToDateTime(row["pInTime"].ToString());
            showPlan.IsPlay = Convert.ToInt32(row["IsPlay"]);
            return showPlan;
        }

        private static List<ShowPlan> GetListShowPlanByDataTable(DataTable dataTable)
        {
            List<ShowPlan> list = new List<ShowPlan>();
            foreach(DataRow row in dataTable.Rows)
            {
                list.Add(GetShowPlanByDataRow(row));
            }
            return list;
        }
        private static List<ShowPlan> GetListShowPlanBySQL(String sql)
        {
            List<ShowPlan> list = new List<ShowPlan>();
            using (DataTable dataTable = DBHelper.GetDataTable(sql))
            {
                list = GetListShowPlanByDataTable(dataTable);
            }
            return list;
        }

        public static List<ShowPlan> GetAllShowPlan()
        {
            string sql = "SELECT * FROM showPlan ORDER BY pInTime";
            return GetListShowPlanBySQL(sql);
        }

        public static ShowPlan GetShowPlanByPID(int pID)
        {
            string sql = "SELECT * FROM showPlan WHERE pID = @pID";
            DataRow row = DBHelper.GetDataTable(sql,new SqlParameter("@pID",pID)).Rows[0];
            return GetShowPlanByDataRow(row);
        }

        /// <summary>
        /// 新增计划
        /// </summary>
        /// <param name="showPlan">展示计划对象</param>
        /// <returns>受影响的行数</returns>
        public static int AddShowPlan(ShowPlan showPlan)
        {
            string sql = "INSERT INTO showPlan(pName,pDesc,pBegin,pEnd) value(@pName,@pDesc,@pBegin,@pEnd)";
            SqlParameter[] par = new SqlParameter[]
            {
                new SqlParameter("@pName",showPlan.PName),
                new SqlParameter("@pDesc",showPlan.PDesc),
                new SqlParameter("@pBegin",showPlan.PBegin),
                new SqlParameter("@pEnd",showPlan.PEnd)
            };

            return DBHelper.GetScalar(sql,par);
        }
    }
}
